package com.xforceplus.bsstool.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xforceplus.bsstool.config.ConfigMain;
import com.xforceplus.bsstool.db.NewUcenterDB;
import com.xforceplus.bsstool.db.OldUcenterDB;
import com.xforceplus.bsstool.log.LogMain;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceStep6 {

  /**
   * 获取老运营组织机构列表
   * @return
   */
  public static List<Map> getOldOrgStructList(Long tenantId,Long companyId,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_org_struct t where t.group_id = ").append(tenantId)
            .append(" and t.company_id = ").append(companyId);
    logger.info("执行老运营组织机构获取，执行SQL: "+sql.toString());
    return OldUcenterDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营组织机构列表
   * @return
   */
  public static Map<String,Map> getNewOrgStructMap(Long tenantId,Long companyId,LogMain logger) throws Exception {
    Map<String,Map> newOrgStructMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_org_struct t where t.tenant_id = ").append(tenantId)
            .append(" and t.company_id = ").append(companyId);
    logger.info("执行新运营组织机构获取，执行SQL: "+sql.toString());
    List<Map> newOrgStructList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newOrgStructMap : newOrgStructList){
      newOrgStructMapMap.put(String.valueOf(newOrgStructMap.get("org_struct_id")),newOrgStructMap);
    }
    return newOrgStructMapMap;
  }

  /**
   * 比对新运营组织机构列表
   * @return
   */
  public static boolean compareOrgStructWithNew(List<Map> oldOrgStructList,Long tenantId,Long companyId,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newOrgStructMapMap  = getNewOrgStructMap(tenantId,companyId,logger);
    for (Map oldOrgStructMap : oldOrgStructList){
      System.out.println("开始执行组织机构比对 组织机构ID:"+oldOrgStructMap.get("org_struct_id"));
      logger.info("开始执行组织机构比对 组织机构ID:"+oldOrgStructMap.get("org_struct_id"));
      //查询新运营对应功能集
      Map newOrgStructMap =newOrgStructMapMap.get(String.valueOf(oldOrgStructMap.get("org_struct_id")));
      if (newOrgStructMap == null){
        logger.warn("组织机构ID:"+oldOrgStructMap.get("org_struct_id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同组织机构ID,继续执行数据信息比对 新运营组织机构："+ JSON.toJSONString(newOrgStructMap));
        //新老数据比对： tenant_id company_id  org_code org_name parent_id  org_type  status audit_status
        oldOrgStructMap.put("tenant_id",oldOrgStructMap.get("group_id"));
        //TODO
        /*
        result = ServiceCommon.compareMapValue(oldOrgStructMap,newOrgStructMap,
                Lists.newArrayList("tenant_id","company_id","org_code","org_name","parent_id","org_type","status","audit_status"),
                "组织机构ID:"+oldOrgStructMap.get("org_struct_id"),
                logger);*/
      }

      //重复组织机构检查
      List<Map> sameOrgStructList = NewUcenterDB.getSqlResult("select * from sys_org_struct where org_struct_id != "+oldOrgStructMap.get("org_struct_id")
              +" and tenant_id = "+oldOrgStructMap.get("tenant_id")
              +" and company_id = "+oldOrgStructMap.get("company_id"));
      if (!CollectionUtils.isEmpty(sameOrgStructList)){
        logger.error("老运营组织机构ID:"+oldOrgStructMap.get("org_struct_id")+" 新运营存在重复组织机构记录个数："+sameOrgStructList.size());
        result = false;
      }
    }
    return result;
  }


  /**
   * 获取老运营组织人员关系列表
   * @return
   */
  public static List<Map> getOldOrgUserRelList(Long tenantId,Long orgId,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_org_user_rel t where t.group_id = ")
            .append(tenantId).append(" and org_struct_id =").append(orgId);
    logger.info("执行老运营组织人员关系获取，执行SQL: "+sql.toString());
    return OldUcenterDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营组织人员关系列表
   * @return
   */
  public static Map<String,Map> getNewOrgUserRelMap(Long tenantId,Long orgId,LogMain logger) throws Exception {
    Map<String,Map> newOrgUserRelMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_org_user_rel t where t.tenant_id = ")
            .append(tenantId).append(" and org_struct_id =").append(orgId);
    logger.info("执行新运营组织人员关系获取，执行SQL: "+sql.toString());
    List<Map> newOrgUserRelList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newOrgUserRelMap : newOrgUserRelList){
      newOrgUserRelMapMap.put(String.valueOf(newOrgUserRelMap.get("id")),newOrgUserRelMap);
    }
    return newOrgUserRelMapMap;
  }

  /**
   * 比对新运营组织人员关系列表
   * @return
   */
  public static boolean compareOrgUserRelWithNew(List<Map> oldOrgUserRelList,Long tenantId,Long orgId,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newOrgUserRelMapMap  = getNewOrgUserRelMap(tenantId,orgId,logger);
    for (Map oldOrgUserRelMap : oldOrgUserRelList){
      System.out.println("开始执行组织人员关系比对 组织人员关系ID:"+oldOrgUserRelMap.get("id"));
      logger.info("开始执行组织人员关系比对 组织人员关系ID:"+oldOrgUserRelMap.get("id"));
      //查询新运营对应功能集
      Map newOrgUserRelMap =newOrgUserRelMapMap.get(String.valueOf(oldOrgUserRelMap.get("id")));
      if (newOrgUserRelMap == null){
        logger.warn("组织人员关系ID:"+oldOrgUserRelMap.get("id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同组织人员关系ID,继续执行数据信息比对 新运营组织人员关系："+ JSON.toJSONString(newOrgUserRelMap));
        //新老数据比对： tenant_id org_struct_id user_id full_selected_flag
        oldOrgUserRelMap.put("tenant_id",oldOrgUserRelMap.get("group_id"));
        result = ServiceCommon.compareMapValue(oldOrgUserRelMap,newOrgUserRelMap,
                Lists.newArrayList("tenant_id","org_struct_id","user_id","full_selected_flag"),
                "组织人员关系ID:"+oldOrgUserRelMap.get("id"),
                logger);
      }

      //重复组织人员关系检查
      List<Map> sameOrgUserRelList = NewUcenterDB.getSqlResult("select * from sys_org_user_rel where id != "+oldOrgUserRelMap.get("id")
              +" and tenant_id = "+oldOrgUserRelMap.get("group_id")
              +" and user_id = "+oldOrgUserRelMap.get("user_id")
              +" and org_struct_id = "+oldOrgUserRelMap.get("org_struct_id"));
      if (!CollectionUtils.isEmpty(sameOrgUserRelList)){
        logger.error("老运营组织人员关系ID:"+oldOrgUserRelMap.get("id")+" 新运营存在重复组织人员关系记录个数："+sameOrgUserRelList.size());
        result = false;
      }
    }
    return result;
  }


}
